package com.kgc.crowd.distributioncrowddatabaseprovider.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.kgc.crowd.distributioncrowdcommon.VO.ProjectVO;
import com.kgc.crowd.distributioncrowdcommon.pojo.*;
import com.kgc.crowd.distributioncrowddatabaseprovider.service.ProjectService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RequestMapping("/database/provider")
@RestController
public class ProjectController {
    @Resource
    ProjectService projectService;

    @RequestMapping("/save/project/remote/{memberId}")
    public ResultEntity<String> saveProjectRemote(
            @RequestBody ProjectVO projectVO,
            @PathVariable("memberId") String memberId) {
        try {
            projectService.saveProject(projectVO, memberId);

            return ResultEntity.successNoData();
        } catch (Exception e) {
            e.printStackTrace();

            return ResultEntity.failed(e.getMessage());
        }
    }
    @RequestMapping("/select/all/type")
    public ResultEntity<List<TypePO>> selectAllType() {
        try {
            List<TypePO> typePOS = projectService.selectTypePo();
            return ResultEntity.successWithData(typePOS);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.failed(e.getMessage());
        }
    }

    @RequestMapping("/select/all/tag")
    public ResultEntity<List<TagPO>> selectAllTag() {
        try {
            List<TagPO> tagPOS = projectService.selectTagPo();
            return ResultEntity.successWithData(tagPOS);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.failed(e.getMessage());
        }
    }
    @RequestMapping("/select/project")
    public ResultEntity<List<ProjectPO>> selectProject() {
        try {
            List<ProjectPO> projectPOS = projectService.selectProjectPo();
            return ResultEntity.successWithData(projectPOS);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.failed(e.getMessage());
        }
    }
    @RequestMapping("/select/projectitempic")
    public ResultEntity<List<ProjectItemPicPO>> selectProjectItemPic(@RequestParam("projectid") Integer projectid){
        try {
            List<ProjectItemPicPO> projectItemPicPOS = projectService.selectProjectItemPicPO(projectid);
            return ResultEntity.successWithData(projectItemPicPOS);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.failed(e.getMessage());
        }
    }
    @RequestMapping("/select/return")
    public ResultEntity<List<ReturnPO>> selectReturnPo(){
        try {
            List<ReturnPO> returnPOS = projectService.selectReturnPo();
            return ResultEntity.successWithData(returnPOS);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.failed(e.getMessage());
        }
    }
    @RequestMapping("/selectById")
    public ResultEntity<List<ProjectPO>> selectById(){
        List<ProjectPO> projectPOS = projectService.selectById(3);
        return ResultEntity.successWithData(projectPOS);
    }
    @RequestMapping("/selectProjectById")
    public ProjectPO selectProjectById(@RequestParam("id") Integer id){
        return projectService.selctProjectById(id);
    }

    @RequestMapping("/UpdateProjectById")
    public Integer UpdateProjectById(@RequestBody ProjectPO projectPO){
        return projectService.UpdateProjectById(projectPO);
    }
    @RequestMapping("/DeleteProjectById")
    public Integer DeleteProjectById(@RequestParam("id")Integer id){
        return projectService.deleteProjectById(id);
    }
    @RequestMapping("/selectProject")
    public ResultEntity<String> selectBooks(@RequestParam(value = "projectName",required = false)String projectName,@RequestParam(value="pageNum", defaultValue="1",required = false)Integer PageNum, @RequestParam(value="pageSize", defaultValue="5",required = false)Integer PageSize){
        PageInfo<ProjectPO> projectPOPageInfo = projectService.selectProject(projectName, PageNum, PageSize);
        String s = JSON.toJSONString(projectPOPageInfo);
        return ResultEntity.successWithData(s);
    }
}
